//
//  RegistView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/27.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class RegistView: UIView {
    @IBOutlet weak var sendLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var pwTextField: UITextField!
    @IBOutlet weak var phoneNumberTextField: UITextField!
    @IBOutlet weak var verificationCodeTextField: UITextField!
    
    func setUI() {
        phoneNumberTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请输入手机号码/邮箱")
        verificationCodeTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请输入您收到的验证码")
        pwTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请输入您的密码")
        timeLabel.layer.cornerRadius = timeLabel.height / 2
        timeLabel.layer.borderColor = timeLabel.textColor.cgColor
        timeLabel.layer.borderWidth = 1
        
        sendLabel.layer.cornerRadius = timeLabel.height / 2
        sendLabel.layer.borderColor = sendLabel.textColor.cgColor
        sendLabel.layer.borderWidth = 1
    }

    @IBAction func sendVCTap(_ sender: UIButton) {
    }
    
    @IBAction func showPwTap(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
}

class BasicInformationView: UIView {
    @IBOutlet weak var nickNameTextField: UITextField!
    @IBOutlet var genderImageViews: [UIImageView]!
    
    func setUI() {
        genderImageViews.first?.isHighlighted = true
        nickNameTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请填写昵称，不可修改")
    }
    
    @IBAction func senderSelectTap(_ sender: UITapGestureRecognizer) {
        let view = sender.view
        if let tapView = view {
            let _ = genderImageViews.map({
                $0.isHighlighted = tapView.subviews.contains($0)
            })
        }
    }
    
    @IBAction func showPWTap(_ sender: UIButton) {
    }
}

class PreferenceView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
    struct SelectData {
        var selectData: [Bool] = []
        init(names: [String]) {
            selectData = names.map({value -> Bool in
                return false
            })
        }
    }
    
    private var datas: SelectData?
    private let itemImageNames = ["btn_ar_pr", "btn_game_pr", "btn_music_pr", "btn_shopping_pr", "btn_life_pr", "btn_movie_pr", "btn_pet_pr", "btn_work_pr", "btn_aliens_pr"]
    private let RoundMargin: CGFloat = 12
    private let itemMargin: CGFloat =  5
    private let itemCount: CGFloat = 3
    private let itemHeght: CGFloat = 67.5
    @IBOutlet weak var mainCollectionView: UICollectionView!
    @IBOutlet weak var mainCollectionViewHeight: NSLayoutConstraint!
    
    func setUI() {
        datas = SelectData(names: itemImageNames)
        let layout = UICollectionViewFlowLayout()
        let itemWidth = (ScreenWidth - RoundMargin * 2 - (itemCount - 1) * itemMargin) / itemCount
        layout.itemSize = CGSize(width: itemWidth, height: itemHeght)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        layout.sectionInset = UIEdgeInsetsMake(0, RoundMargin, 0, RoundMargin)
        mainCollectionViewHeight.constant = RoundMargin * 2 + itemHeght * itemCount + (itemCount - 1) * itemMargin
        mainCollectionView.collectionViewLayout = layout
        mainCollectionView.delegate = self
        mainCollectionView.dataSource = self
        mainCollectionView.register(UINib(nibName: "\(PreferenceCollectionViewCell.self)", bundle: nil), forCellWithReuseIdentifier: "\(PreferenceCollectionViewCell.self)")
        mainCollectionView.reloadData()
    }
    
    // MARK:- UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return itemImageNames.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "\(PreferenceCollectionViewCell.self)", for: indexPath) as? PreferenceCollectionViewCell
        cell?.showData(isSelect: datas!.selectData[indexPath.row], imageName: itemImageNames[indexPath.row])
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let isSelect = datas!.selectData[indexPath.row]
        datas?.selectData[indexPath.row] = !isSelect
        collectionView.reloadData()
    }
}

class ForgeThePasswordView: UIView {
    @IBOutlet weak var phoneNumberTextField: UITextField!
    @IBOutlet weak var verificationCodeTextField: UITextField!
    @IBOutlet weak var sendCodeLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBAction func sendTap(_ sender: UIButton) {
        
    }
    
    func setUI(){
        phoneNumberTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请输入手机号码/邮箱")
        phoneNumberTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请输入您收到的验证码")
        
        timeLabel.layer.cornerRadius = timeLabel.height / 2
        timeLabel.layer.borderColor = timeLabel.textColor.cgColor
        timeLabel.layer.borderWidth = 1
        
        sendCodeLabel.layer.cornerRadius = timeLabel.height / 2
        sendCodeLabel.layer.borderColor = sendCodeLabel.textColor.cgColor
        sendCodeLabel.layer.borderWidth = 1
    }
}

class ResetPasswordView: UIView {
    @IBOutlet weak var newPWTextField: UITextField!
    @IBOutlet weak var confirmThePasswordTextField: UITextField!
    
    func setUI() {
        newPWTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请输入至少8位数密码")
        confirmThePasswordTextField.attributedPlaceholder = LoginViewController.placeholderStr(text: "请再次输入密码")
    }
    
    @IBAction func showPwTap(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    
    @IBAction func problemTap(_ sender: UIButton) {
        
    }
}


